﻿using Loaf.Beans;
using Loaf.ViewModels;
using Loaf.自定义控件.Controls;
using Prism.Commands;
using System.Windows.Media;

namespace Loaf.Views
{
    /// <summary>
    /// ReaderWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ReaderWindow : CustomWindow
    {
        public Context Context { get; set; }
        public ReaderWindowModel Model { get; set; }


        public ReaderWindow(Context context)
        {
            InitializeComponent();

            Context = context;

            //返回主页按钮
            this.HomeWindowCommand = new DelegateCommand(() =>
            {
                Context.BookSetting.LastBook = null;
                Context.WriteContext();
                this.Close();
                Context.MainWindow.Show();
            });

            Model = new ReaderWindowModel(Context);
            this.DataContext = Model;
            Model.ScrollViewer = this.ContentScrollViewer;


            Model.OnLoaded();
        }


        /// <summary>
        /// 滚动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ContentScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            var addOffset = e.Delta / 2;
            Context.BookSetting.LastBook.Offset = this.ContentScrollViewer.VerticalOffset - addOffset;
            this.ContentScrollViewer.ScrollToVerticalOffset(this.ContentScrollViewer.VerticalOffset - addOffset);
            var offset = VisualTreeHelper.GetOffset(this.SplitLineControl);
            if (this.ContentScrollViewer.VerticalOffset - addOffset > offset.Length)
            {
                var sub = this.ContentScrollViewer.VerticalOffset - addOffset - offset.Length;

                Model.GetNextChapter();

                this.ContentScrollViewer.ScrollToVerticalOffset(sub);
            }
        }

    }
}
